import AsyncStorage from '@/utils/AsyncStorage';
import global from '@/utils/global';
import { create } from 'mobx-persist';
import { inject } from 'mobx-react';
import alarmPushStore, { AlarmPush, AlarmPush_Store_Key } from './alarmPush';
import appStateStore, { AppState, AppState_Store_Key, IThemeMode } from './appState';
import deviceDataStore, { DeviceData, DeviceData_Store_Key } from './deviceData';
import deviceDetailStore, { DeviceDetail, DeviceDetail_Store_Key } from './deviceDetail';
import deviceListStore, { DeviceList, DeviceList_Store_Key } from './deviceList';
import deviceShareStore, { DeviceShare, DeviceShare_Store_Key } from './deviceShare';
import homesStore, { Homes, Home_Store_Key } from './homes';
import infraRedConfigStore, { InfraRedConfig, InfraRedConfig_Store_Key } from './infraRedConfig';
import languageStore, { Language, Language_Store_Key } from './language';
import messagesStore, { Messages, Message_Store_Key } from './messages';
import userStateStore, { UserState, UserState_Store_Key } from './userState';
import wallwtStore, { Wallet, Wallet_Store_Key } from './wallet';

global.cleanData = () => {
  deviceListStore.cleanData();
  userStateStore.cleanData();
  homesStore.cleanData();
  alarmPushStore.cleanData();
  deviceDetailStore.cleanData();
}

const hydrate = create({
  storage: AsyncStorage,
});

export interface IStore {
  [Language_Store_Key]: Language,
  [DeviceData_Store_Key]: DeviceData,
  [AppState_Store_Key]: AppState,
  [DeviceList_Store_Key]: DeviceList,
  [DeviceDetail_Store_Key]: DeviceDetail,
  [Home_Store_Key]: Homes,
  [UserState_Store_Key]: UserState,
  [AlarmPush_Store_Key]: AlarmPush,
  [Message_Store_Key]: Messages,
  [DeviceShare_Store_Key]: DeviceShare,
  [Wallet_Store_Key]: Wallet,
  [InfraRedConfig_Store_Key]: InfraRedConfig,
}

export interface IInjectProps extends Partial<IStore> {

}

export interface INorInjectProps extends IInjectProps {
  languageKey: string;
  themeMode: IThemeMode;
}

type IInject = typeof inject;

const norInject: IInject = (...p) => {
  // console.log('norInject', ...p);
  if (typeof p[0] === "function" || p[0] === undefined) {
    const fn = p[0];
    const newP = (stores: IStore) => ({
      languageKey: stores[Language_Store_Key].key,
      themeMode: stores[AppState_Store_Key].themeMode,
      ...fn ? fn(stores) : {},
    });
    return inject(newP);
  } else if (p instanceof Array) {
    const newP = (stores: IStore) => {
      const mapInfo: IInjectProps = {};
      p.forEach(key => stores[key] && (mapInfo[key] = stores[key]));
      // console.log(mapInfo);
      return {
        languageKey: stores[Language_Store_Key].key,
        themeMode: stores[AppState_Store_Key].themeMode,
        ...mapInfo,
      }
    };
    // console.log('Array', newP);
    return inject(newP);
  }
  return inject(...p);
}

/**
 * 缓存
 * 有顺序问题
 * 需要考虑
 */
const initCache = async ({
  deviceList: deviceListCache, deviceData: deviceDataCache, homes: homesCache,
}: any) => {
  await hydrate('homes', homesStore, homesCache || '{}');
  await hydrate('deviceList', deviceListStore, deviceListCache || '{}');
  console.log('deviceDataCache', deviceDataCache);
  if (deviceDataCache) {
    hydrate('deviceData', deviceDataStore, deviceDataCache || '{}');
  }
};


export { norInject, initCache };

function createStores(): IStore {
  return {
    [DeviceData_Store_Key]: deviceDataStore,
    [Language_Store_Key]: languageStore,
    [AppState_Store_Key]: appStateStore,
    [DeviceList_Store_Key]: deviceListStore,
    [DeviceDetail_Store_Key]: deviceDetailStore,
    [Home_Store_Key]: homesStore,
    [UserState_Store_Key]: userStateStore,
    [AlarmPush_Store_Key]: alarmPushStore,
    [Message_Store_Key]: messagesStore,
    [DeviceShare_Store_Key]: deviceShareStore,
    [Wallet_Store_Key]: wallwtStore,
    [InfraRedConfig_Store_Key]: infraRedConfigStore,
  };
}


const stores = createStores();
export default stores;
